Skip to main content

Exception handling

Don’t use a try-catch statement for most exception handling. Place global exception handler (middleware, exception filter, etc) in your code.

In C#, using a global exception handler instead of try-catch blocks can be a useful approach for handling exceptions in a more streamlined way. When using try-catch blocks, each block must be specifically defined for each method or section of code that could potentially throw an exception. This can lead to repetitive and cluttered code, making it harder to read and maintain. A global exception handler, on the other hand, can provide a central location for handling exceptions that occur throughout the application. By using a global exception handler, all unhandled exceptions can be caught and handled in a consistent manner, reducing the amount of code needed and simplifying error reporting and logging. However, it's important to note that a global exception handler should not be used as a catch-all solution and should still be complemented with proper error handling and defensive coding practices.